import MyRequestAxios from "./request";
import { appConfig } from "@/config";

const failAuth = (err) => {
    console.log("err");
};

const failCodeMap = new Map([
    [400, { msg: "请求错误" }],
    [401, { msg: "未认证", handler: failAuth }],
    [403, { msg: "无权限" }],
    [404, { msg: "请求内容为空" }],
    [405, { msg: "请求方式错误" }],
    [408, { msg: "请求超时" }],
    [422, { msg: "参数错误" }],
    [500, { msg: "服务器内部错误" }],
    [501, { msg: "服务未实现" }],
    [502, { msg: "网关错误" }],
    [503, { msg: "服务不可用" }],
    [504, { msg: "网关超时" }],
    [505, { msg: "HTTP版本不受支持" }],
]);

MyRequestAxios.useErrorMapCode(failCodeMap);
MyRequestAxios.useTokenConfig(appConfig.token);

class ServiceRequest extends MyRequestAxios {
    constructor(...params) {
        super(...params);
    }

    // 预留token刷新
    autoUpdateToken(ctx) {}

    request(options = {}) {
        // options.url = options.url[0] === "/" ? options.url : `/${options.url}`;
        if (!options.method) options.method = "GET";
        return super
            .request(options)
            .then((response) => {
                this.autoUpdateToken(response);
                return response && response.data;
            })
            .catch((err) => {
                return Promise.reject(err);
            });
    }
}

// 针对内网

// 不是内网
export const service = new ServiceRequest({
    baseURL: appConfig.baseUrl.default,
    headers: {
        "content-type": "application/x-www-form-urlencoded",
    },
});
